package com.victualler.business.inventory.service.impl;

import com.victualler.business.inventory.dto.InventoryOrderInfoDto;
import com.victualler.business.inventory.dto.print.InventoryPrintDto;
import com.victualler.business.inventory.entity.InventoryItemDao;
import com.victualler.business.inventory.entity.InventoryOrderDao;
import com.victualler.business.inventory.repository.InventoryOrderQueryRepository;
import com.victualler.business.inventory.repository.InventoryOrderRepository;
import com.victualler.business.inventory.service.InventoryItemService;
import com.victualler.business.inventory.service.InventoryOrderService;
import com.victualler.business.purchase.enums.CheckStateEnum;
import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.common.enums.ChecKDataErrorEnum;
import com.victualler.core.config.dict.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class InventoryOrderServiceImpl extends BaseServiceImpl<InventoryOrderDao,InventoryOrderRepository,InventoryOrderQueryRepository> implements InventoryOrderService {

    @Autowired
    private InventoryItemService inventoryItemService;

    @Autowired
    private DictService dictService;

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  根据条件入参prePurchaseRequestDto动态分页查询权限
     */
    public InventoryOrderInfoDto findInfo(Long id) {
        InventoryOrderDao inventoryOrderDao = this.getById(id);
        List<InventoryItemDao> inventoryItemDaoList = this.inventoryItemService.getByParentId(id);
        return new InventoryOrderInfoDto(inventoryOrderDao,inventoryItemDaoList);
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param inventoryOrderInfoDto 预订单实体大对象
     * @Function  保存预订单实体大对象
     */
    @Transactional
    public void saveInfo(InventoryOrderInfoDto inventoryOrderInfoDto) {
        //如果是提交类型 则更新提交时间
        if (CheckStateEnum.UN_CHECK.getKey().equals(String.valueOf(inventoryOrderInfoDto.getInventoryOrderDao().getCheckState()))) {
            inventoryOrderInfoDto.getInventoryOrderDao().setCommitDate(new Date());
        }
        if(inventoryOrderInfoDto.getInventoryOrderDao().getId() == null) {
            //生成订单号
            this.save(inventoryOrderInfoDto.getInventoryOrderDao());
            String orderNo = "ST" + new SimpleDateFormat("yyyyMMdd").format(new Date()) +"-"+ inventoryOrderInfoDto.getInventoryOrderDao().getId();
            inventoryOrderInfoDto.getInventoryOrderDao().setOrderNo(orderNo);
            //刷新字表数据
            for (InventoryItemDao inventoryItemDao : inventoryOrderInfoDto.getInventoryItemDaoList()) {
                inventoryItemDao.setParentId(inventoryOrderInfoDto.getInventoryOrderDao().getId());
            }
            this.inventoryItemService.saveAll(inventoryOrderInfoDto.getInventoryItemDaoList());
        } else {
            //更新
            this.save(inventoryOrderInfoDto.getInventoryOrderDao());
            //更新明细信息
            this.inventoryItemService.updateList(inventoryOrderInfoDto.getInventoryItemDaoList(),inventoryOrderInfoDto.getInventoryOrderDao().getId());
        }
    }

    /**
     * @author Guoshichang
     * @since 18:01 on 2018/10/9.
     * @param inventoryOrderDao 审查大对象
     * @return
     * @Function
     */
    @Transactional
    public void check(InventoryOrderDao inventoryOrderDao) {
        InventoryOrderDao saveObject = this.getById(inventoryOrderDao.getId());
        saveObject.setCheckDate(new Date());
        saveObject.setCheckUser(inventoryOrderDao.getCheckUser());
        saveObject.setCheckUserCode(inventoryOrderDao.getCheckUserCode());
        saveObject.setCheckRemark(inventoryOrderDao.getCheckRemark());
        saveObject.setCheckState(inventoryOrderDao.getCheckState());
        this.save(saveObject);
    }


    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id 预订单id
     * @Function  删除草稿订单
     */
    @Transactional
    public void deleteInfo(Long id) {
        InventoryOrderDao inventoryOrderDao = this.getById(id);
        //只有草稿数据允许删除
        if(CheckStateEnum.TEMP.getKey().equals(String.valueOf(inventoryOrderDao.getCheckState()))) {
            //删除所有信息
            this.deleteById(id);
            this.inventoryItemService.deleteByParentId(id);
        } else {
            String stateName =  CheckStateEnum.getValue(String.valueOf(inventoryOrderDao.getCheckState()));
            throw ChecKDataErrorEnum.createException(ChecKDataErrorEnum.DATA_ERROR,stateName + "状态不可删除!");
        }
    }


    /**
     * @author Guoshichang
     * @since 12:46 on 2018/11/14.
     * @param orderNo 订单号
     * @return 输出pdf的列表
     * @Function
     */
    public List<InventoryPrintDto> getExportModelList(String orderNo) {
        //查到List
        List<InventoryPrintDto> list = this.baseQueryRepository.getExportModelList(orderNo);
        //字典值
        String codeValue = dictService.getDictByCodeType("StorageType",list.get(0).getStorageType()).getCodeValue();
        String formatDate = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm").format(new Date());
        list.forEach(item -> {
            item.setStorageType(codeValue);
            item.setPrintDate(formatDate);
        });
        return list;
    }
}
